home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / regexp / regsub.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  84 lines

  1. /*
  2.  * regsub @(#)regsub.c    1.3 of 2 April 86 
  3.  *
  4.  * Copyright (c) 1986 by University of Toronto. Written by Henry Spencer.  Not
  5.  * derived from licensed software. 
  6.  *
  7.  * Permission is granted to anyone to use this software for any purpose on any
  8.  * computer system, and to redistribute it freely, subject to the following
  9.  * restrictions: 
  10.  *
  11.  * 1. The author is not responsible for the consequences of use of this
  12.  * software, no matter how awful, even if they arise from defects in it. 
  13.  *
  14.  * 2. The origin of this software must not be misrepresented, either by explicit
  15.  * claim or by omission. 
  16.  *
  17.  * 3. Altered versions must be plainly marked as such, and must not be
  18.  * misrepresented as being the original software. 
  19.  */
  20. #include <stdio.h>
  21. #ifdef AMIGA
  22. #include "regexp.h"
  23. #else
  24. #include <regexp.h>
  25. #endif
  26. #include "regmagic.h"
  27.  
  28. #ifndef CHARBITS
  29. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  30. #else
  31. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  32. #endif
  33.  
  34. /*
  35.  * - regsub - perform substitutions after a regexp match 
  36.  */
  37. void
  38. regsub(prog, source, dest)
  39.     regexp         *prog;
  40.     char           *source;
  41.     char           *dest;
  42. {
  43.     register char  *src;
  44.     register char  *dst;
  45.     register char   c;
  46.     register int    no;
  47.     register int    len;
  48.     extern char    *strncpy();
  49.  
  50.     if (prog == NULL || source == NULL || dest == NULL) {
  51.     regerror("NULL parm to regsub");
  52.     return;
  53.     }
  54.     if (UCHARAT(prog->program) != MAGIC) {
  55.     regerror("damaged regexp fed to regsub");
  56.     return;
  57.     }
  58.     src = source;
  59.     dst = dest;
  60.     while ((c = *src++) != '\0') {
  61.     if (c == '&')
  62.         no = 0;
  63.     else if (c == '\\' && '0' <= *src && *src <= '9')
  64.         no = *src++ - '0';
  65.     else
  66.         no = -1;
  67.  
  68.     if (no < 0) {        /* Ordinary character. */
  69.         if (c == '\\' && (*src == '\\' || *src == '&'))
  70.         c = *src++;
  71.         *dst++ = c;
  72.     } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  73.         len = prog->endp[no] - prog->startp[no];
  74.         (void) strncpy(dst, prog->startp[no], len);
  75.         dst += len;
  76.         if (len != 0 && *(dst - 1) == '\0') {    /* strncpy hit NUL. */
  77.         regerror("damaged match string");
  78.         return;
  79.         }
  80.     }
  81.     }
  82.     *dst++ = '\0';
  83. }
  84.